<?php
class photoModel {
	public $base;
	public $db;
	/**
	 * 构造函数 连接数据库
	 *
	 * @param object $base
	 */
	function __construct($base) {
		$this->base = $base;
		$this->db = $this->base->connect();
	}
	public function getAlbumName($name) {
		$query = "select album_name from album where album_name= '$name'";
		$result = $this->db->query($query);
		return $result ? true :false;
	}
	
	public function getTagByName($name) {
		$query = "select tag_name from tag where tag_name = $name ";
		$result = $this->db->query($query);
		return $result ? true :false;
	}
	
	public function getUserAlbum() {
		$query = "select album_name from album where user_id = '1'";
		$result = $this->db->query($query); 
		$output = "<select>";
		while($row = $result->fetch_array()) {
	 		$output .= "<option value=\"\">".$row['album_name']."</option>";
		} 
	  	$output .= "</select>";
		return $output;
	}
	/**
	*查找最新的图片
	*
	*/
	public function getNewPhoto() {
		$query = "select * from upload order by upload_time desc limit 0,6";
		$result = $this->db->query($query);
		while($row = $result->fetch_array()) {
			$new_photo[] = $row;
		}
		return $new_photo;
	}
	
	/**
	 * 将title desc priv插入到数据库之中，now()为创建的时间
	 *
	 */
	public function insertToAlbum($title,$desc,$priv) {
		$query = "insert into album set album_name = '$title',album_desc = '$desc',album_privilege = '$priv',album_create_time = now()";
		$result = $this->db->query($query);
		return $this->db->insert_id;
	}
	
	/**
	 * 将tag_name插入到数据库中
	 * now()为创建的时间
	 */
	public function insertToTag($value) {
		$query = "insert into tag set tag_name = '$value',tag_create_time = now()";
		$result = $this->db->query($query);
		return $this->db->insert_id;
	}
	/**
	 * 将上传的信息(名字 类型 大小) 插入到数据库upload之中
	 * @return upload_id
	 */
	public function insertToUpload($name,$type,$size) {
		$query = "insert into upload set upload_name = '$name',upload_type = '$type', upload_size = '$size', upload_time = now()";
		$result = $this->db->query($query);
		return $this->db->insert_id;
	}
	/**
	*将album_id和tag_id写入到数据库表album_tag中
	*
	*/
	public function insertToAlbumAndTag($album_id,$tag_id) {
		$query = "insert into album_tag set album_id = '$album_id', tag_id = '$tag_id'";
		$result = $this->db->query($query);
		return $result ? true : false;
		
	}
	/**
	*将upload_id和tag_id写入到数据库表upload_tag中
	*@retune 
	*/
	public function insertToUploadTag($upload_id,$tag_id) {
		$query = "insert into upload_tag set upload_id = '$upload_id', tag_id = '$tag_id'";
		$result = $this->db->query($query);
		return $result ? true : false;
	}
	/**
	*将描述插入到表description中
	*
	*/
	public function insertToDesc($desc,$upload_id) {
		$query = "insert into description set upload_id = '$upload_id',desc_content = '$desc',desc_time = now()";
		$result = $this->db->query($query);
		return $result ? true : false;
	}
	/**
	*更改数据库表upload中的priv
	*
	*/
	public function updateUpByid($upload_id,$priv) {
		if($priv == '好友查看') {
			$query = "update upload set upload_priv = '1' where uplaod_id = '$upload_id'";
		} elseif($priv == '群组查看') {
			$query = "update upload set upload_priv = '2' where upload_id = '$upload_id'";
		} else {
			return false;
		}
		$result = $this->db->query($query);
		return $result ? true : false;
	}
	/**
	*将上传图片的路径写入到数据库表upload中
	*
	*/
	public function updatePathById($path,$upload_id) {
		$query = "update upload set upload_path = '$path' where upload_id = '$upload_id'";
		$result = $this->db->query($query);
		return $result ? true : false;
	}
	/**
	*将上传的信息删除
	*
	*/
	public function deleteUploadById($upload_id) {
		$query = "delete from upload where upload_id = $upload_id";
		$result = $this->db->query($query);
		return $result ? true : false;
	}
	
}
?>